import { Component } from '@angular/core';

import { NavParams, ViewController } from 'ionic-angular';

import { TaskDetailService } from './detail.service';

import { Config } from '../../../providers/config';

import _ from 'lodash';


@Component({
  selector: 'page-task-emp',
  templateUrl: './emp.html',
  providers: [TaskDetailService]
})
export class EmpPage {
	moa_page_name: any = "page-task-emp";
  moa_page_explain: any = "流程 - 选择员工";

  // segment: empList（人员列表） empTree（通讯录）
  //segment = "empList";

  //参与人
  empnames;
  empnamesId;

  // 默认参与人
  defaultEmpnames =[];
  defaultEmpnamesId =[];

  // 是否显示组织数，0 不显示 1显示
  isSelAddr;
  // 是否单选， true 单选， false 多选
  isSelectOne;

  // 人员选择对象数组{"empname":,"empnameId":,"isChecked":}
  empList = [
  ];

  // 是否全选
  isCheckAll = false;

  constructor(
    public navParams: NavParams,
    public viewCtrl: ViewController
  ) {
    this.isSelAddr = this.navParams.get('isSelAddr');
    this.isSelectOne = this.navParams.get('isSelectOne');
    this.empnames = this.navParams.get('empnames');
    this.empnamesId = this.navParams.get('empnamesId');
    this.defaultEmpnames = this.navParams.get('defaultEmpnames');
    this.defaultEmpnamesId = this.navParams.get('defaultEmpnamesId');

    // 构建人员选择对象数组
    for (var i = 0; i < this.empnames.length; i++) {
      let temp = {
        "empname": this.empnames[i],
        "empnameId": this.empnamesId[i],
        "isChecked": _.indexOf(this.defaultEmpnamesId, this.empnamesId[i]) > -1 ? true : false
      }
      this.empList.push(temp);
    }
  }

  complete() {
    // let result = _.filter(this.listData, { isSelected: true });
    this.dismiss({ "defaultEmpnames": this.defaultEmpnames, "defaultEmpnamesId": this.defaultEmpnamesId });
  }

  dismiss(data?: any) {
    // using the injected ViewController this page
    // can "dismiss" itself and pass back data
    this.viewCtrl.dismiss(data);
  }

  //原来checked 状态是ture的改成false并在默认选择数组中移除,
  //状态false的改成true并添加到默认选择数组
  selectChange(index) {
    if (!this.isSelectOne) { // 多选
      if (this.empList[index].isChecked) {
        this.empList[index].isChecked = false;
        this.defaultEmpnames = _.pull(this.defaultEmpnames, this.empList[index].empname);
        this.defaultEmpnamesId = _.pull(this.defaultEmpnamesId, this.empList[index].empnameId);
      } else {
        this.empList[index].isChecked = true;
        this.defaultEmpnames.push(this.empList[index].empname);
        this.defaultEmpnamesId.push(this.empList[index].empnameId);
      }
    } else { // 单选
      if (this.empList[index].isChecked) {
        this.empList[index].isChecked = false;
        this.defaultEmpnames = [];
        this.defaultEmpnamesId = [];
      } else {
        if(this.defaultEmpnamesId.length > 0) { // 将已选的改成未选
          _.find(this.empList,{"empnameId" : this.defaultEmpnamesId[0]}).isChecked = false;
        }

        this.empList[index].isChecked = true;
        this.defaultEmpnames = _.concat([],this.empList[index].empname);
        this.defaultEmpnamesId =_.concat([],this.empList[index].empnameId);
      }
    }
  }

  /**
   * [checkALLorNot description]
   */
  checkALLorNot(){
    var that = this;
    this.isCheckAll = !this.isCheckAll;

     _.forEach(this.empList, function(data){
        data.isChecked = that.isCheckAll;
      })

    if(this.isCheckAll){ // 全选
      this.defaultEmpnames = this.empnames;
      this.defaultEmpnamesId = this.empnamesId;

    } else { // 取消选择
      this.defaultEmpnames=[];
      this.defaultEmpnamesId=[];
    }

  }

  // onSegmentChange(segmentEvent) {

  //   //this.segment = this.segment;
  // };

}
